export default class Avatar extends createjs.Sprite {

  startX = 0;
  startY = 0;

  pointValue = 1;

  disappear = false;
  moving = false;
  isnew = false;

  direction = '';
  delta = 10;

  game = null;

  constructor(spriteSheet, game, { frame, col, row, size }) {
    super(spriteSheet);
    this.gotoAndStop(frame);
    this.frame = frame;
    this.col = col;
    this.row = row;
    this.w = size;
    this.h = size;
    this.game = game;
    this.scaleX = 0.9;
    this.scaleY = 0.9;

    this.regX = -40;
    this.regY = -50;

    this.on('tick', this.handleTick.bind(this));
    this.on('animationend', this.handleAnimationFinish.bind(this));

    this.on('mousedown', (evt) => {
      if (!game.lock) {
        this.startX = evt.stageX;
        this.startY = evt.stageY;
      }
    });

    this.on('pressup', (evt) => {
      if (this.startX && this.startY && !game.lock && (Math.abs(this.startX - evt.stageX) > 10 || Math.abs(this.startY - evt.stageY) > 10)) {
        game.loadSound('slide');
        //  左右
        if (Math.abs(this.startX - evt.stageX) > Math.abs(this.startY - evt.stageY)) {
          if (this.startX < evt.stageX) { // → 右

            if (this.col !== game.colNum - 1) {
              game.lock = true;
              game.changeItem(1, this);
            }
          } else { // ← 左

            if (this.col !== 0) {
              game.lock = true;
              game.changeItem(2, this);
            }

          }
        } else { // 上下

          if (this.startY < evt.stageY) { // ↓ 下
            if (this.row !== game.rowNum - 1) {
              game.lock = true;
              game.changeItem(3, this);
            }

          } else { // ↑ 上
            if (this.row !== 0) {
              game.lock = true;
              game.changeItem(4, this);
            }
          }
        }
      }


      return false;
    });
  }


  handleTick(e) {
    const item = e.currentTarget;
    const { row, col } = item;
    const { game } = this;

    if (item.disappear) {

      if (item.alpha > 0) {
        item.alpha -= 0.08;
      } else {
        game.score += this.pointValue;
        //console.log('得分 + ', this.pointValue, game.score);
        game.stage.removeChild(item);
        game.matrixArray[item.row][item.col] = {
          row, col
        };
        game.updateArr(item.col, item.row);
      }
    }

    if (item.moving) {

      const { col, row } = item;
      const thisX = item.w * col;
      const thisY = item.h * row;
      // console.log('moving', item.col, item.row, item.direction);

      // 右移
      if (item.direction === 'right') {
        if (item.x < thisX) {
          item.x += item.delta;
        } else if (item.x > thisX) {
          item.x = thisX;
        } else {
          this.resetStage(item);
        }
      }
      // 左移
      if (item.direction === 'left') {
        if (item.x > thisX) {
          item.x -= item.delta;
        } else if (item.x < thisX) {
          item.x = thisX;
        } else {
          this.resetStage(item);
        }
      }
      // 上移
      if (item.direction === 'up') {
        if (item.y > thisY) {
          item.y -= item.delta;
        } else if (item.y < thisY) {
          item.y = thisY;
        } else {
          this.resetStage(item);
        }
      }
      // 下移
      if (item.direction === 'down') {
        if (item.y < thisY) {
          item.y += item.delta;
        } else if (item.y > thisY) {
          item.y = thisY;
        } else {
          this.resetStage(item);
        }
      }
    } else {
      // 下落

      const checkAll2 = () => {
        if (row === 7 && col === 7 && (game.dropCount !==0 || game.lock)) {
          // debugger;
          // console.log('checkAll 2 === === === === === === === === === === === === === === === === === === 2= =2');
          // alert('game.lock=' + game.lock + '  game.dropCount=' + game.dropCount);
          game.dropCount = 0;
          game.lock = false;
        }
      };

      let thisY = item.h * item.row;
      if (Math.ceil(item.y) !== thisY) {
        createjs.Tween.get(item, {
          override: true
        }).to({ y: Math.abs(thisY)}, 120).call(function() {
          game.matrixArray[item.row][item.col] = item;
          game.dropCount--;
          checkAll2();
        });

      } else {
        item.y = Math.ceil(item.y);
      }
    }

  }

  handleAnimationFinish() {
    this.stop();
  }

  resetStage(item) {
    const { game } = this;
    item.moving = false;

    if (game.needReset) {
      game.reset = true;
      game.needReset = false;
    }
    if (game.needMove.length > 0) {
      game.remove = true;
    }
  }

}
